//
//  NetworkTools.m
//  OC中AFN封装 同时支持post和get
//
//  Created by 王、小胜 on 16/7/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NetworkTools.h"

//网络工具类协议
@protocol NetworkToolsProxy <NSObject>

///网络请求的方法
//method HTTP请求方法
//URLString URLString
//parameters 参数字典
//sucess 成功回调
//failure 失败回调
@optional
-(NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                     urlStriung:(NSString *)urlString
                                     parameters:(id)parameters
                                        success:(void(^)(NSURLSessionDataTask *,id))success
                                        failure:(void (^)(NSURLSessionDataTask*,NSError *))failure;

@end
//遵守网络协议。骗过xcode给一个智能提示，以及编译通过
@interface NetworkTools ()<NetworkToolsProxy>

@end

@implementation NetworkTools

+(instancetype)sharedTools{
    static NetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL *baseUrl=[NSURL URLWithString:@"http://httpbin.org/"];
        tools = [[self alloc]initWithBaseURL:baseUrl];
        
        //设置反序列化
        tools.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
    });
    return tools;
    
}

-(void)request:(requestMethod)method urlString:(NSString *)urlString parameters:(id)parameters finished:(void (^)(id, NSError *))finished
{
    NSString *methodName=(method ==GET) ? @"GET":@"POST"?
    
    // dataTaskWithHTTPMethod本类没有实现方法，但是父类实现了
    // 在调用方法的时候，如果本类没有提供，直接调用父类的方法，AFN 内部已经实现！
//    [self dataTaskWithHTTPMethod:methodName urlStriung:urlString parameters:parameters success:^(NSURLSessionDataTask * task, id responseObject) {
//        
//        finished(responseObject,nil);
//        
//    } failure:^(NSURLSessionDataTask * task, NSError * error) {
//        finished(nil,error);
//    } resume];
    
    [[self dataTaskWithHTTPMethod:methodName urlStriung:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        finished(responseObject,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        finished(nil,error);
    }] resume];
    
    
//    if (method==GET) {
//        [self GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            
//            finished(responseObject,nil);
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            
//            finished(nil,error);
//        }];
//    }else{
//        [self POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            
//            finished(responseObject,nil);
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            
//            finished(nil,error);
//        }];
//    }
    
    
    
}
@end
